﻿
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace AutoMapper关系映射例子
{
    public static class Common
    {
        public static List<PersonDTO> ToDTO(IList<Person> value)
        {
            if (value == null)
            {
                return new List<PersonDTO>();
            }

            return AutoMapper.Mapper.Map<List<PersonDTO>>(value);
        }

        public static PersonDTO ToDTO<T>(T value)
        {
            if (value == null)
            {
                return new PersonDTO();
            }

            return AutoMapper.Mapper.Map<PersonDTO>(value);

        }

        /// <summary>
        /// DTO转换
        /// </summary>
        /// <typeparam name="TSource">原始类型</typeparam>
        /// <typeparam name="TDTO">转换成的类型</typeparam>
        /// <param name="p"></param>
        /// <param name="d"></param>
        /// <returns></returns>
        public static TDTO ToDTO<TSource,TDTO>(TSource p,TDTO d)
            where TDTO:new()
        {
            if (p == null)
            {
                return new TDTO();
            }

            return AutoMapper.Mapper.Map<TDTO>(p);
        }



    }
}
